package com.subfg.common.entity.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.subfg.common.util.common.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "公共返回结果信息")
@JsonInclude(value=JsonInclude.Include.NON_NULL)
public class CommonResult<T> implements Serializable {
    @ApiModelProperty(value = "接口操作返回状态码, 0 以上表示成功 0以下表示失败", name = "code", required = true)
    private int code;
    @ApiModelProperty(value = "接口返回的描述信息, 有可能不返回", name = "message", required = false)
    private String message;
    @ApiModelProperty(value = "新token 如果该值存在, 则需替换旧token", name = "token", required = false)
    private String token;
    @ApiModelProperty(value = "接口操作返回值封装的数据", name = "data", required = true)
    private T data;
    @ApiModelProperty(value = "服务器时间戳, 如遇不同时区, 则需要客户端进行额外计算", name="timestamp", required = true)
    private Long timestamp;

    public CommonResult(int code) {
        this.code = code;
    }

    public CommonResult(T data) {
        this.code = 200;
        this.data = data;
        this.timestamp = TimeUtil.getCurrentTime();
    }

    public CommonResult(int code, T data) {
        this.code = code;
        this.data = data;
        this.timestamp = TimeUtil.getCurrentTime();
    }

    public CommonResult(int code, String token, T data) {
        this.code = code;
        this.token = token;
        this.data = data;
        this.timestamp = TimeUtil.getCurrentTime();
    }

    public CommonResult(int code, String message) {
        this.code = code;
        this.message = message;
        this.timestamp = TimeUtil.getCurrentTime();
    }

    public static CommonResult ok(){return new CommonResult(200);}

    public static <T> CommonResult<T> ok(T data) {
        return new CommonResult(data);
    }

    public static <T> CommonResult<T> ok(String message, T data) {
        return new CommonResult(200, message, data);
    }

    public static <T> CommonResult<T> ok(int code, String message) {return new CommonResult(code, message);}

    public static <T> CommonResult<T> ok(int code, T data) {
        return new CommonResult(code, data);
    }

    public static <T> CommonResult<T> ok(int code, String token, T data) {
        return new CommonResult(code, token, data);
    }

    public static <T> CommonResult<T> okay(int code, String success) {
        Map<String, Object> map = new HashMap<>();
        map.put("dataMessage", success);
        return new CommonResult(code, map);
    }





    public static <T> CommonResult<T> error(Integer code, String errorMessage) {
        return new CommonResult(code, errorMessage);
    }


    public static <T> CommonResult<T> error(Integer code, String errorMessage, T data) {
        return new CommonResult(code, errorMessage, data);
    }

    public static <T> CommonResult<T> error(String errorMessage) {
        return new CommonResult(-300, errorMessage);
    }

    public static <T> CommonResult<T> error() {
        return new CommonResult(-300, "request.message.error");
    }
}
